import nme.display.MovieClip;
import nme.events.Event;
import nme.media.Sound;
import nme.media.SoundChannel;
import nme.Assets;
import haxe.Timer;

class Orchestra extends MovieClip { 

    public var home:Home;
    public var instrument:SoundChannel;
    public var notes:Array<Dynamic>;
    public var hums:Array<Hum>;
    public var timer:Timer;
    public var position:Int;

    public function new(myHome:Home) {
        super();

        // Declare the home
        home = myHome;

        // Position is the column that will be played next
        position = 0;

        // Create the scale of notes utilizing classes contained in the external lib.swf, which is included during compilation by build.hxml
        notes = new Array<Dynamic>();
        notes[0] = Assets.getSound ("assets/c5.mp3");
        notes[1] = Assets.getSound ("assets/b4.mp3");
        notes[2] = Assets.getSound ("assets/as4.mp3");
        notes[3] = Assets.getSound ("assets/a4.mp3");
        notes[4] = Assets.getSound ("assets/gs4.mp3");
        notes[5] = Assets.getSound ("assets/g4.mp3");
        notes[6] = Assets.getSound ("assets/fs4.mp3");
        notes[7] = Assets.getSound ("assets/f4.mp3");
        notes[8] = Assets.getSound ("assets/e4.mp3");
        notes[9] = Assets.getSound ("assets/ds4.mp3");
        notes[10] = Assets.getSound ("assets/d4.mp3");
        notes[11] = Assets.getSound ("assets/cs4.mp3");
        notes[12] = Assets.getSound ("assets/c4.mp3");
        /// Set the number of rows equal to number of notes
        home.rowNum = notes.length;

        // Create the array which will contain hums
        hums = [];

        // Create the instrument, which will be used by playColumn()
        instrument = new SoundChannel();

        // Create the timer, running three times a second, which plays a column of notes
        timer = new Timer(333);
        timer.run = playColumn;
    } // public function new(myCanvas:MovieClip)

    public function playColumn() {
        // Run by the timer
        
        // Turn off highlighting
        for(hum in 0...hums.length) for(row in 0...home.rowNum) for(column in 0...home.colNum) hums[hum].boxes[row][column].alpha = 1;

        // Play the current column
        for(hum in 0...hums.length) for(row in 0...home.rowNum) {
            // Determine the column to highlight. Due to latency, the user hears the previous column
            var highlight = position - 1;

            /// If negative, we want to  highlight last column
            if(highlight < 0) highlight = home.colNum - 1;

            // Highlight the appropriate column
            hums[hum].boxes[row][position].alpha = 0.75; 

            // Play each box in the current column
            if(hums[hum].score[row][position]) instrument = notes[row].play(); 
        }
        
        // Increment the column to be played
        position = (position + 1) % home.colNum;
    } // public function playColumn()

    public function newHum(myX:Float, myY:Float, mySize:Float) { hums.push(new Hum(home, myX, myY, mySize)); }

} // class Orchestra extends MovieClip
